export default {
  reminderModal(err) {
    console.error('reminderModal --->', err)
    if (!err) return
    if (err && this.skipError(JSON.stringify(err || {}))) return
    if (err && err?.msg?.includes('已经存在')) return

    let content = ''
    if (err && err?.msg?.includes('timeout')) {
      content = '服务器超时，请重试'
    } else {
      content = JSON.stringify(err?.errMsg || err?.msg || err)
    }
    uni.showModal({
      title: '温馨提示',
      content,
      showCancel: false,
      confirmText: '好哒',
    })
  },

  reminderToast(title) {
    if (!title) return
    uni.showToast({
      title,
      icon: 'none',
      duration: 2500,
    })
  },

  getType(data) {
    return Object.prototype.toString.apply(data).slice(8, -1)
  },

  setStorageSession(data) {
    const newData = {
      ...data,
      create_time: parseInt(Date.now() / 1000.0),
    }
    return uni.setStorageSync('__session__', JSON.stringify(newData))
  },

  getStorageSession() {
    return uni.getStorageSync('__session__')
  },
  getFileType(url) {
    if (!url) return

    const isPrefix = url.indexOf('imageView2')
    const IMAGE_TYPES = ['.png', '.jpg', '.jpeg', '.gif', 'webp']
    const VIDEO_TYPES = ['.mp4', '.mov', '.avi', '.3gp', '.m3u8']

    if (VIDEO_TYPES.some((type) => url.endsWith(type))) {
      return 'video'
    } else if (isPrefix > 0 || IMAGE_TYPES.some((type) => url.endsWith(type))) {
      return 'image'
    }
  },

  // 获取服务提供商
  // https://uniapp.dcloud.net.cn/api/plugins/provider.html
  getProvider(service_type) {
    const SERVICE_TYPES = ['oauth', 'share', 'payment', 'push']

    if (!service_type) {
      this.reminderToast('缺少参数service_type params')
      throw new Error('缺少参数service_type params')
    }

    if (service_type && !SERVICE_TYPES.includes(service_type)) {
      this.reminderToast('错误的service_type值')
      throw new Error('错误的service_type值')
    }

    return new Promise((resolve, reject) => {
      uni.getProvider({
        service: service_type,
        success: (res) => {
          resolve(res.provider)
        },
        fail: (err) => {
          reject(err)
        },
      })
    })
  },

  skipError(err) {
    const SKIP_ERROR = [
      "toast can't be found",
      'showLoading',
      'updateTextView:fail',
      'insertTextView:fail',
      'insertImageView:fail',
      'updateImageView:fail',
      'removeTextView:fail',
      'removeImageView:fail',
      'no advertisement',
      'updateVideoPlayer:fail',
      'removeVideoPlayer:fail',
      'getUserProfile:fail',
      'operateVideoPlayer:fail',
      'requestPayment:fail',
      'requestPayment:fail cancel',
      'navigateBack:fail',
      'navigateBack:fail no page',
      'insertVideoPlayer:fail',
      'operateWXDataForAd:fail',
      'jsapi has no permission',
      'network interrupted error',
      'request:fail -7:net::ERR_TIMED_OUT',
      'cronet component error',
      'ERR_CERT_COMMON_NAME_INVALID',
      'ERR_CONNECTION_REFUSED',
      'request:fail interrupted',
      'ERR_CONNECTION_RESET',
    ]

    return SKIP_ERROR.some((item) => err.indexOf(item) > -1)
  },

  isPhoneNumber(e) {
    const i = '134,135,136,137,138,139,150,151,152,157,158,159,187,188,147,182,183,184,178'
    const n = '130,131,132,155,156,185,186,145,176'
    const a = '133,153,180,181,189,177,173,170'
    const o = e || ''
    const r = o.substring(0, 3)
    const d = o.substring(0, 4)
    const s =
      !!/^1\d{10}$/.test(o) &&
      (n.indexOf(r) >= 0
        ? '联通'
        : a.indexOf(r) >= 0
          ? '电信'
          : d == '1349'
            ? '电信'
            : i.indexOf(r) >= 0
              ? '移动'
              : '未知')
    return s
  },

  isEmail(email) {
    return !!/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(email)
  },

  wait(ms) {
    ms = Math.max(ms, 1)
    return new Promise((resolve) => setTimeout(resolve, ms))
  },
}
